<?
$fdat = '';
$url = "http://www.siye.co.uk/siye/viewuser.php?uid=".$_GET['aid'];


	$fdat =  file ( $url );
	while(list($key,$val)=each($fdat)){
		//$summary = true;
		if(stristr($val, "viewstory.php")){
			$collection = $collection +1;
			$valA = explode('<a href="viewstory.php?sid=',trim($val));
			$valA = explode('">',$valA[1]);			
			$dataA[$collection]['id']=$valA[0];

			$valB = explode('<a href="viewuser.php?uid=',trim($valA[1]));			
			$dataA[$collection]['title']=trim(str_replace('</a> by','',$valB[0]));

			$valB = explode('">',$valB[1]);			
			$dataA[$collection]['author_id']=$valB[0];
			$valC = explode('</a>',$valA[2]);
			$dataA[$collection]['author']=$valC[0];

			$summary = false;
		}
		if(stristr($val, "Chapters: ")){
			$valA = explode('Chapters: ',trim($val));
			$chA = explode('-',$valA[1]);
			if($collection!=0){
				$dataA[$collection]['chapters'] = trim($chA[0]);
			}
			$summary = false;
		}
		if(stristr($val, "Published: ")){
			$valA = explode('Published: ',trim($val));
			$chA = explode('-',$valA[1]);
			if($collection!=0){
				$dataA[$collection]['published'] = trim(str_replace('.','-',$chA[0]));
			}
			$summary = false;
		}
		if(stristr($val, "Updated: ")){
			$valA = explode('Updated: ',trim($val));
			$chA = explode('-',$valA[1]);
			if($collection!=0){
				$dataA[$collection]['updated'] = trim(str_replace('.','-',$chA[0]));
			}
			$summary = false;
		}
		if(stristr($val, '<div class="desc">')){
			$summary = false;
		}		
		if($summary){
			if($collection!=0){
				$dataA[$collection]['summaryT'] .= trim(str_replace('<br>','',$val));
			}	
		}
		if(stristr($val, '</a>]')){
			$summary = true;
		}
		
/*	
		if(stristr($val,'<font')){
			$summary = false;
		}
	
*/
	}
	$dataB = $dataA;
	while(list($key,$val)=each($dataB)){
		$temp = explode('<br>',$val['summaryT']);
		//echo htmlspecialchars($temp[0])."<hr>";
		
				$dataA[$key]['summary'] .= trim($temp[0]);
			

	}	

echo "&nbsp;&nbsp;&nbsp;Scan Complete<br>";
$cnt = 0; $cnt_n = 0; $cnt_u = 0; $cnt_Q = 0; $cnt_i = 0;

/*echo "<pre>";
var_dump($dataA);
echo "</pre>";
exit();	*/
	while(list($key,$val)=each($dataA)){
		$sql = "INSERT INTO `fic_listing` ( `sitekey`, `story_id` , `author_id` , `story_title` , `chapters` , `published` , `updated` , `status`,`summary`,`inserted` ) VALUES ('S','".$val['id']."', '".$val['author_id']."', '".str_replace("'","''",$val['title'])."', '".$val['chapters']."', '".$val['published']."', '".$val['updated']."', '?','".str_replace("'","''",$val['summary'])."', NOW() )";
		//echo $sql."<hr>";
		$cnt = $cnt + 1;
		$rs1 = $db->Execute($sql);
		if($rs1===false){ 
			$err = $db->ErrorMsg();
			if(stristr($err,'Duplicate entry')){
				//echo "Possible Update:";
				//echo $val['id'];
				$sqlc = "SELECT * FROM fic_listing WHERE sitekey='S' and story_id='".$val['id']."'";
				$rsC = $db->Execute($sqlc);
				$upd = false;
				if($rsC->Fields('chapters')!=$val['chapters']){ $upd = true; }
				if($rsC->Fields('updated')!=$val['updated']){ $upd = true; }
				$insid = $rsC->Fields('id');
				$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='224'");
	
				
				if($upd){
						$sqT = "chapters='".$val['chapters']."',updated='".$val['updated']."',inserted=NOW()";
						if($rsC->Fields('published')=='0000-00-00'){
							$sqT .= ", published='".$val['published']."'";						
						}
						if($rsC->Fields('author_id')=='0'){
							$sqT .= ", author_id='".$val['author_id']."'";						
						}
						if($rsC->Fields('summary')!=$val['summary']){
							$sqT .= ", summary='".str_replace("'","''",$val['summary'])."'";	
						}	
						if($rsC->Fields('story_title')!=$val['title']){
							$sqT .= ", story_title='".str_replace("'","''",$val['title'])."'";	
						}
						//echo $sqT."<br>";	
					if($rsC->Fields('status')=='?' || $rsC->Fields('status')=='N'){
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='S' and story_id='".$val['id']."'";
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')!='I'){	
						$cnt_u = $cnt_u + 1;
						$sqlU = "UPDATE fic_listing SET ".$sqT.",status='U' WHERE sitekey='S' and story_id='".$val['id']."'";						
						$db->Execute($sqlU);
					}elseif($rsC->Fields('status')=='I'){	
						$cnt_i++;
						$sqlU = "UPDATE fic_listing SET ".$sqT." WHERE sitekey='S' and story_id='".$val['id']."'";						
						$db->Execute($sqlU);
					}
				}
				//echo "<br>";
			}else{
				echo "<b>".$err."</b>:".$sql;
			}
		}else{
			//if new story try to insert author		
			$insid = $db->Insert_ID();
			$db->Execute("INSERT INTO fic_metaverse SET fic_id='".$insid ."',universe_id='224'");
			$cnt_n = $cnt_n + 1;
			$sql2 = "INSERT INTO `fic_authors` ( `sitekey`,`author_id` , `author_name` )VALUES ('S','".$val['author_id']."', '".str_replace("'","''",$val['author'])."')";
			$rs2 = $db->Execute($sql2);
			
		}
	}
echo '&nbsp;&nbsp;&nbsp;Update Completed<br>';
echo '&nbsp;&nbsp;&nbsp;total entries:'.$cnt.', ignored:'.$cnt_i.', new stories:'.$cnt_n.', updated new:'.$cnt_Q.', UPDATED:'.$cnt_u.'<br>';
?>
